package com.dg11185.nearshop.activity.home;

import java.util.ArrayList;

import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.android.nearshop.R;
import com.dg11185.nearshop.activity.PrimaryLevelActivity;
import com.dg11185.nearshop.app.StaticData;
import com.dg11185.nearshop.view.ExpandTabView;
import com.dg11185.nearshop.view.TwoLevelCatalogView;
import com.dg11185.nearshop.view.TwoLevelCatalogView.OnSelectListener;
import com.wlper.lib.uitls.ViewUtils;

/**
 * 主页
 * 
 * @author wanliang527
 * @date 2014-5-5
 * @deprecated
 */
public class HomeActivity extends PrimaryLevelActivity implements
        OnCheckedChangeListener {

//    private ScrollView mScrollView;
    private GridView shopType, newestActivities;
    private RadioGroup mRadioGroup;
//    private ViewPager mViewPager;
    private LayoutInflater inflater;
//    private LinearLayout scrollContent;
    private TabHost mTabHost;
    private ViewStub newestActivitiesView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        initView();

    }

    private void findViewsId() {
        shopType = (GridView) findViewById(R.id.grid_v_shop_type);
        mRadioGroup = (RadioGroup) findViewById(R.id.ra_group_tabs);
        mTabHost = (TabHost) findViewById(R.id.tab_host);
        newestActivitiesView = (ViewStub) findViewById(R.id.view_newest_activities);

//        mScrollView = (ScrollView) findViewById(R.id.scroll_v);
//        scrollContent = (LinearLayout) findViewById(R.id.llayout_scroll_content);
    }

    private void initView() {
        findViewsId();
        // 初始化商家类型选择
        shopType.setAdapter(new SimpleAdapter(this, StaticData
                .getHomeShopTypes(), R.layout.shop_type_grid_v_item,
                new String[] { "name", "icon" }, new int[] {
                        R.id.txt_v_shop_type, R.id.image_v_shop_type_icon }));
        shopType.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // **********商家类型的点击事件监听***********//

                TextView txtView = (TextView) view
                        .findViewById(R.id.txt_v_shop_type);
                ViewUtils.showToast(HomeActivity.this, txtView.getText()
                        .toString().trim());
            }
        });
        // 初始化精选商家和会员日商家
        mTabHost.setup();
        View view = inflater.inflate(R.layout.view_shops_on_vip_day, null);
        view.setId(0);
        mTabHost.addTab(mTabHost.newTabSpec("精选商家").setIndicator("精选商家")
                .setContent(tabContent));
        mTabHost.addTab(mTabHost.newTabSpec("会员日商家").setIndicator("会员日商家")
                .setContent(tabContent));
        // mTabHost.addTab(mTabHost.newTabSpec("精选商家").setIndicator("精选商家")
        // .setContent(new Intent(this, FeaturedShopsActvity.class)));
        // mTabHost.addTab(mTabHost.newTabSpec("会员日商家").setIndicator("会员日商家")
        // .setContent(new Intent(this, ShopsOnVipDayActivity.class)));
        mRadioGroup.check(R.id.ra_btn1);
        mRadioGroup.setOnCheckedChangeListener(this);

        showNewestActyView();

    }

    private TabHost.TabContentFactory tabContent = new TabHost.TabContentFactory() {

        @Override
        public View createTabContent(String tag) {
            View view = null;
            if (tag.equals("精选商家")) {
                view = inflater.inflate(R.layout.view_featured_shops, null);

            } else if (tag.equals("会员日商家")) {
                view = inflater.inflate(R.layout.view_shops_on_vip_day,
                        null);
            }
            return view;
        }
    };

    @Override
    protected void initTopBanner() {
        super.initTopBanner();
        LinearLayout leftView = getPartView(LEFT);

        final ExpandTabView mExpandTabView = new ExpandTabView(this);
        ArrayList<View> viewList = new ArrayList<View>();
        final TwoLevelCatalogView reginSpinnerView = new TwoLevelCatalogView(
                this, StaticData.getRegionFirLevCateLog(),
                StaticData.getRegionSecLevCatelog());
        reginSpinnerView.setOnSelectListener(new OnSelectListener() {

            @Override
            public void getValue(View view,int firLevPos, int secLevPos, String showText) {
                String text = showText;
                if (showText.equals("不限")) {
                    text = "全城";
                }
                onRefresh(reginSpinnerView, text);

            }

            private void onRefresh(View view, String showText) {
                mExpandTabView.onPressBack();
                mExpandTabView.setTitle(showText, 0);
                // ************选择地区后的UI更新操作在此进行*******************//
                ViewUtils.showToast(HomeActivity.this, showText);
            }
        });

        viewList.add(reginSpinnerView);
        String[] tabNames = new String[] { "全城" };

        mExpandTabView.initView(viewList, tabNames);
        leftView.addView(mExpandTabView, new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
        ((LinearLayout.LayoutParams) mExpandTabView.getLayoutParams()).leftMargin = 20;

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.ra_btn1:
                mTabHost.setCurrentTabByTag("精选商家");
                break;

            case R.id.ra_btn2:
                mTabHost.setCurrentTabByTag("会员日商家");
                break;
        }

    }

    /**
     * 显示最新活动视图。根据接口数据决定是否要显示
     * 
     */
    public void showNewestActyView() {
        if (newestActivities == null) {
            newestActivitiesView.inflate();
            newestActivities = (GridView) findViewById(R.id.grid_v_newest_activities);
            newestActivities.setAdapter(new SimpleAdapter(this, StaticData
                    .getNewestActivity(), R.layout.view_activities_grid_v_item,
                    new String[] { "imageUrl" },
                    new int[] { R.id.image_v_activities_item }));
            newestActivities.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    // *******点击活动的事件添加**********//

                    ViewUtils.showToast(HomeActivity.this, "第" + (position + 1)
                            + "个活动");

                }
            });
        }
    }

}
